﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;

namespace Gao.Poker.App.Win
{
    /// <summary>
    /// Interaction logic for ConsumptionWin.xaml
    /// </summary>
    public partial class ConsumptionWin : Window
    {
        public ConsumptionViewModel ViewModel { get; set; }
        
        public ConsumptionWin()
        {
            InitializeComponent();
            ViewModel = new ConsumptionViewModel();
            ViewModel.Message = OnMessage;
            DataContext = ViewModel;
        }

        private void OnMessage(string obj)
        {
            MessageBox.Show(obj, "提示消息");
        }

        private void MemberSearch_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.OnPropertyChanged("Member");
        }

        private void ProductSearch_Click(object sender, RoutedEventArgs e)
        {

            //ViewModel.OnPropertyChanged("Product");
           ViewModel.GetProduct();
        }

        private void dgRight_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            ViewModel.OnPropertyChanged("Monetary");
        }

        private void dgRight_LostFocus(object sender, RoutedEventArgs e)
        {
            ViewModel.OnPropertyChanged("Monetary");
        }

        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            if(ViewModel.Member == null )
            {
                OnMessage("请输入会员号");
                return;
            }

            Consumption con = new Consumption();
            con.MemberCardNo = ViewModel.Member.CardNo;
            con.MemberName = ViewModel.Member.Name;
            con.Monetary = ViewModel.Monetary;
            con.CreateDt = DateTime.Now;

            foreach (var ph in ViewModel.ConsumptionProducts)
            {
                var cp = new ConsumptionProduct();
                cp.BarCode = ph.BarCode;
                cp.Discount = ph.Discount;
                cp.Number = ph.Number;
                cp.Price = ph.PPrice;
                cp.ProductName = ph.ProductName;
                con.ConsumptionPrlducts.Add(cp);
            }

            ConsumptionService.Instance().Save(con);
            OnMessage("销售成功");
            Close();
        }
    }
}
